package com.example.springcloudindex.web;

import com.example.springcloudindex.bean.User;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;

@RestController
@RefreshScope  // 开启当前类的配置刷星
public class OrderAction {

    @RequestMapping("order")
    public String order(HttpServletRequest request){
        int port = request.getServerPort();
        return "order: " + port + " : " + request.getHeader("myname");
    }
    @RequestMapping("hello")
    public String hello(String name){
        return "hello " + name;
    }

    @RequestMapping("hello/{name}")
    public String hello1(@PathVariable("name") String name){
        return "你好 " + name;
    }
    @RequestMapping("add")
    public int add(@RequestParam int a, @RequestParam int b){
        return a + b;
    }

    @RequestMapping("showMe")
    public User showMe(@RequestBody User user, @RequestParam String newName){
        user.setAge(user.getAge() + 10);
        user.setName(newName);
        return user;
    }

    @Value("${upload.mapper.image}")
    String uploadImage;

    @Value("${upload.mapper.doc}")
    String uploadDoc;

    @RequestMapping(path = "showConfig",
            produces = MediaType.TEXT_HTML_VALUE+";charset=utf-8")
    String showConfig(){
        return "uploadImage = " + uploadImage + "<br>" +
                "uploadDoc = " + uploadDoc;
    }
}
